/*
 * this part is experimental part, you may build your own template.
 * 
 */

package retrival;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.Event;
import java.util.Date;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.*;
/**
 *
 * @author Administrator
 */
public class Experiments extends JPanel
{
    StockPrice[] prices;
    int pCount,TL;
    private int[] PointL,PointH1,PointH2,PointH4,PointH3,Pattern,PIP1,PIP2,PIP3,PIP4;
    private int[] Xp,XPL,XPH1,XPH2,XPH3,XPH4,Xpattern,Xpaa,Xpip1,Xpip2,Xpip3,Xpip4;

    private int[] Yp,YPL,YPH1,YPH2,YPH3,YPH4,Ypattern,Ypaa,Ypip1,Ypip2,Ypip3,Ypip4;
    private double[] PAA;

    Experiments(TPsOnSlidingWindow s)
    {
       prices = s.prices;
       pCount = s.priceCount;

       Xp = new int[pCount];

       PointL = s.PointLevel1;

        PointH1 =  s.TPFinal1;
        PointH2 =  s.TPFinal2;
        PointH3 =  s.TPFinal3;
        PointH4 =  s.TPFinal4;

        Pattern = s.TPPattern10;

       PAA = s.PAA;

       PIP1 = s.PIP1;
       PIP2 = s.PIP2;
       PIP3 = s.PIP3;
       PIP4 = s.PIP4;

       TL = s.TL;

    }
    public void paintComponent(Graphics g)
   {
      super.paintComponent(g);

      Graphics2D g2 = (Graphics2D) g;

      double leftX = 0;
      double topY = 0;
      double width = 1800;
      double height = 1000;

      Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
      g2.setPaint(Color.WHITE);
      g2.fill(rect);

      caculateXY();

      g2.setColor(Color.BLACK);
      BasicStroke   stroke=new   BasicStroke(2);
      g2.setStroke(stroke);

      int up = 35;
      g2.setColor(Color.BLACK);


      g2.drawPolyline(XPH1, YPH1, XPH1.length);
      g2.drawPolyline(XPH2, YPH2, XPH2.length);
      g2.drawPolyline(XPH3, YPH3, XPH3.length);
      g2.drawPolyline(XPH4, YPH4, XPH4.length);

       g2.drawString("WS=0"+"", Xp[0]-20,Yp[0]+up);
      g2.drawString("WS=2"+"", XPH1[0]-20,YPH1[0]+up);
      g2.drawString("WS=3"+"", XPH2[0]-20,YPH2[0]+up);
      g2.drawString("WS=6"+"", XPH3[0]-20,YPH3[0]+up);
      g2.drawString("WS=11"+"", XPH4[0]-20,YPH4[0]+up);


      g2.drawPolyline(Xp, Yp, pCount);
 
     //pip
       g2.setColor(Color.black);

      g2.drawRect(40, 100, 810, 545);
       for(int i =0;i<Xp.length+1;)
      {
          g2.drawString(i+" ", Xp[i],660);
          i+=300;
      }

       g2.drawString(Xp.length +" ", Xp[Xp.length-1],660);

        int[] error = new int[4];
        error[0] = AllError(PointH1);
        error[1] = AllError(PointH2);
        error[2] = AllError(PointH3);
        error[3] = AllError(PointH4);
        printerror("C:\\SlidingWindow\\ErrorProducedBy_TPofSlidingWindow.txt",error);

      int [] temp1 = countPeriodTP(PointH1);
      int [] temp2 = countPeriodTP(PointH2);
      int [] temp3 = countPeriodTP(PointH3);
      int [] temp4 = countPeriodTP(PointH4);
      print("C:\\SlidingWindow\\countTPsOnEqualPeriodSW1_TP.txt",temp1);
      print("C:\\SlidingWindow\\countTPsOnEqualPeriodSW2_TP.txt",temp2);
      print("C:\\SlidingWindow\\countTPsOnEqualPeriodSW3_TP.txt",temp3);
      print("C:\\SlidingWindow\\countTPsOnEqualPeriodSW4_TP.txt",temp4);

    }

     private void print(String s,int[] count)
    {
        try
      {
       FileWriter fileWriter=new FileWriter(s);
       for (int i = 0; i < count.length; i++) {
        fileWriter.write(String.valueOf(i+1)+" "+String.valueOf(count[i])+"\r\n");
         }
       fileWriter.flush();
       fileWriter.close();
       }catch (Exception e) {
             e.printStackTrace();
       }
    }

      public void printerror  (String s, int[] arr1)
    {
      try
      {
       FileWriter fileWriter=new FileWriter(s);

       fileWriter.write("The first column is representation level;" +"\r\n The second column is the error produced by the representation method"+"\r\n");
       for (int i = 0; i < arr1.length; i++) {
           fileWriter.write(i+" "+String.valueOf(arr1[i])+ "\r\n");
         }
       fileWriter.flush();
       fileWriter.close();
       }catch (Exception e) {
             e.printStackTrace();
       }
    }
      
    private int[] countPeriodTP(int[] TP)
    {
        int[] Count = new int[6];
        for(int i =0;i<Count.length;i++)
            Count[i] = 0;
        for(int i =0;i < TP.length;i++)
        {
            if(TP[i]<=490)
                Count[0]++;
            else if(TP[i]<=980)
                 Count[1]++;
            else if(TP[i]<=1470)
                 Count[2]++;
            else if(TP[i]<=1960)
                 Count[3]++;
            else if(TP[i]<=2450)
                 Count[4]++;
            else
                Count[5]++;
        }
         return Count;
    }

    private int AllError(int[] Xhuff)
    {
       double error = 0;
       for(int i = 1;i<Xhuff.length;i++)
       {
           error += CaculateError(Xhuff[i-1],Xhuff[i]);
       }
       return (int)(error/4000);
   }

   private double CaculateError(int i, int j)
   {
        double error = 0;
        for(int k = i;k<j;k++)
           error += Math.abs(prices[i].Price + ( prices[j].Price-prices[i].Price) *((k-i) / (j-i)) - prices[k].Price);
        return error;
   }

    private void caculateXY()
    {
       int D=425;//;490;
       double Xlength =0.3;//0.5;//1504/pCount;//0.39;
       double Scale=-0.01;//-0.01;
       int n = 75,w=-55;

       Yp = new int[pCount];

       int o= 0;

       for(int i =0;i<pCount;i++)
        {
           Xp[i] =(int) (10+i*Xlength-w);
           Yp[i] = (int) Math.round (Scale* prices[i].Price+D+0*n);
        }

        YPL = new int[PointL.length];
        XPL = new int[PointL.length];
        for(int i =0;i<PointL.length;i++)
        {
            YPL[i] = (int)Math.round (Scale*prices[PointL[i]].Price+D+1*n);
            XPL[i] =(int)(10+ PointL[i]*Xlength-w);
        }

         YPH1 = new int[PointH1.length];
         XPH1 = new int[PointH1.length];
        for(int i =0;i<PointH1.length;i++)
        {
            YPH1[i] = (int)Math.round (Scale*prices[PointH1[i]].Price+D+1*n);
            XPH1[i] =(int)(10+ PointH1[i]*Xlength-w);
        }

         YPH2 = new int[PointH2.length];
         XPH2 = new int[PointH2.length];
        for(int i =0;i<PointH2.length;i++)
        {
            YPH2[i] = (int)Math.round (Scale*prices[PointH2[i]].Price+D+2*n);
            XPH2[i] =(int)(10+ PointH2[i]*Xlength-w);
        }
         YPH3 = new int[PointH3.length];
         XPH3 = new int[PointH3.length];
        for(int i =0;i<PointH3.length;i++)
        {
            YPH3[i] = (int)Math.round (Scale*prices[PointH3[i]].Price+D+3*n);
            XPH3[i] =(int)(10+ PointH3[i]*Xlength-w);
        }
         YPH4 = new int[PointH4.length];
         XPH4 = new int[PointH4.length];
        for(int i =0;i<PointH4.length;i++)
        {
            YPH4[i] = (int)Math.round (Scale*prices[PointH4[i]].Price+D+4*n);
            XPH4[i] =(int)(10+ PointH4[i]*Xlength-w);
        }
         Ypip1 = new int[PIP1.length];
         Xpip1 = new int[PIP1.length];
        for(int i =0;i<PIP1.length;i++)
        {
            Ypip1[i] = (int)Math.round (Scale*prices[PIP1[i]].Price+D+1*n);
            Xpip1[i] =(int)(10+ PIP1[i]*Xlength-w);
        }
           Ypip2 = new int[PIP2.length];
         Xpip2 = new int[PIP2.length];
        for(int i =0;i<PIP2.length;i++)
        {
            Ypip2[i] = (int)Math.round (Scale*prices[PIP2[i]].Price+D+2*n);
            Xpip2[i] =(int)(10+ PIP2[i]*Xlength-w);
        }
           Ypip3 = new int[PIP3.length];
         Xpip3 = new int[PIP3.length];
        for(int i =0;i<PIP3.length;i++)
        {
            Ypip3[i] = (int)Math.round (Scale*prices[PIP3[i]].Price+D+3*n);
            Xpip3[i] =(int)(10+ PIP3[i]*Xlength-w);
        }
           Ypip4 = new int[PIP4.length];
         Xpip4 = new int[PIP4.length];
        for(int i =0;i<PIP4.length;i++)
        {
            Ypip4[i] = (int)Math.round (Scale*prices[PIP4[i]].Price+D+4*n);
            Xpip4[i] =(int)(10+ PIP4[i]*Xlength-w);
        }

    }

}
